package com.softserveinc.interactive_board.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.softserveinc.interactive_board.commons.CheckedUsersList;
import com.softserveinc.interactive_board.domain.User;
import com.softserveinc.interactive_board.service.UserService;
import com.softserveinc.interactive_board.validators.form.EditUserForm;

@Controller
@RequestMapping("/administration/users")
public class AdminUsersController {

	private static final String USERS_FILE_LOCATION = "administration/users/index";
	private static final String ADD_USER_FILE_LOCATION = "administration/users/add";
	private static final String EDIT_USER_FILE_LOCATION = "administration/users/edit";
	
	private static final String ADD_USERFORM_ATTR_NAME = "editUserForm";
	private static String EDIT_USERFORM_ATTR_NAME = "editUserForm";
	
	private static final String USERS_ATTR_NAME = "checkedUsersList";
	private static final String USERTYPES_ATTR_NAME = "userTypesAttr";

	
	@RequestMapping(method = RequestMethod.GET)
	public String def() {
		return "redirect:/administration/users/";
	}
	
	
	@RequestMapping("/")
	public String users(Model model) {
		model.addAttribute(AdminUsersController.USERS_ATTR_NAME, new CheckedUsersList(userService.getUsers()));
		return AdminUsersController.USERS_FILE_LOCATION;
	}
	
	
	@RequestMapping("/filter")
	public String users(@RequestParam("user_type_id") int typeId, Model model) {
		model.addAttribute(AdminUsersController.USERS_ATTR_NAME, new CheckedUsersList(userService.getUsersByType(typeId)));
		return AdminUsersController.USERS_FILE_LOCATION;
	}

	
	//----------------------------Add-------------------------------------
	@RequestMapping(method = RequestMethod.GET, value = "/add")
	public String usersAdd(Model model) {
		model.addAttribute(AdminUsersController.ADD_USERFORM_ATTR_NAME, new EditUserForm());
		model.addAttribute(AdminUsersController.USERTYPES_ATTR_NAME, this.userService.getRegistratebleUserTypes());
		return AdminUsersController.ADD_USER_FILE_LOCATION;
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/add")
	public String usersAdd(Model model, @Valid final EditUserForm form, final BindingResult result) {
		if (result.hasErrors()) {
			model.addAttribute(AdminUsersController.USERTYPES_ATTR_NAME, this.userService.getRegistratebleUserTypes());
			return AdminUsersController.ADD_USER_FILE_LOCATION;
		}
		this.userService.addUser(form.getUser());
		return "redirect:/administration/users/";
	}
	
	//----------------------------Edit------------------------------------
	@RequestMapping(method = RequestMethod.GET, value = "/edit")
	public String usersEdit(@RequestParam("user_id") Long userId, Model model) {
		User user = null;
		if ((user = this.userService.getUser(userId)) == null)
			return "redirect:/administration/users";
		model.addAttribute(AdminUsersController.EDIT_USERFORM_ATTR_NAME, new EditUserForm(user));
		model.addAttribute(AdminUsersController.USERTYPES_ATTR_NAME, this.userService.getRegistratebleUserTypes());
		return AdminUsersController.EDIT_USER_FILE_LOCATION;
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/edit")
	public String usersEdit(Model model, @Valid final EditUserForm form, final BindingResult result) {
			if (result.hasErrors()) {
				model.addAttribute(AdminUsersController.USERTYPES_ATTR_NAME, this.userService.getRegistratebleUserTypes());
				return AdminUsersController.EDIT_USER_FILE_LOCATION;
			}
			this.userService.updateUser(form.getUser());
			return "redirect:/administration/users/";
	}
	
	
	//----------------------------Delete-------------------------------------
	@RequestMapping("/delete")
	public String userDelete(@RequestParam("user_id") Long userId) {
		this.userService.deleteUser(userId);
		return "redirect:/administration/users";
	}
	
	@RequestMapping("/deletegroup")
	public String usersGroupDelete(@ModelAttribute(AdminUsersController.USERS_ATTR_NAME) CheckedUsersList list) {
		List<User> buff = list.getCheckedUsers(); 
		if (!buff.isEmpty())
			this.userService.deleteUsers(buff);
		return "redirect:/administration/users";
	}


	private UserService userService;
	
	@Autowired
	@Qualifier("userServiceImpl")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
